#include "../../include/ManageSmartCar.h"

using namespace std;


CarController* carController;

void manageSmartCar(CarController* _carController) {
    carController = _carController;

    string cmd;     // 输入命令
    while (cmd != E_X_I_T ) {

        cout << "========主界面===========" << endl;
        cout << "指令说明:" << endl;
        cout << "1. createCar 创建小车" << endl;
        cout << "2. listCar   列出所有小车ID" << endl;
        cout << "3. editCar   编辑小车" << endl;
        cout << "4. deleteCar 删除小车" << endl;
        cout << "5. storeData 保存小车数据" << endl;
        cout << "6. loadData  加载小车数据" << endl;
        cin >> cmd;
        if(cmd == "1" || cmd == CREATE_CAR) {
            createCar();
        }
        else if(cmd == "2" || cmd == LIST_CAR) {
            listCar();
        }
        else if(cmd == "3" || cmd == EDIT_CAR) {
            editCar();
        }
        else if(cmd == "4" || cmd == DELETE_CAR) {
            deleteCar();
        }
        else if(cmd == "5" || cmd == STORE_DATA) {
            storeCarData();
        }
        else if(cmd == "6" || cmd == LOAD_DATA) {
            if(carController->getCarCount()!=0) {
                cout << "当前小车数量不为0,为避免冲突,请勿直接导入数据。" << endl;
            }
            else {
                loadCarData();
            }
        }
        cout << "=======================" << endl;
    }
    cout << "已退出程序。" << endl;
    return;
}

SmartCar* getCarByNum() {
    int length = carController->getCarCount();
    SmartCar* carList[length];
    carController->listAllCar(carList);
    cout << "共有" << length << "辆小车:" << endl;
    for(int i=0;i<length;i++) {
        cout << i+1 << " : " << carList[i]->getId() << endl;
    }

    int carNum = -1;
    while(true) {
        cout << "请输入小车序号:";
        cin >> carNum;
        cout << endl;
        carNum -= 1;
        if(carNum >= 0 && carNum <length) {
            break;
        }
        cout << "序号有误" << endl;
    }
    return carList[carNum];
}
void createCar() {
    string cmd;
    while(cmd != E_X_I_T) {
        cmd = "";
        int length = carController->getCarCount();
        cout << "请选择创建小车的方式:" << endl;
        cout << "1. directCreate 直接创建小车" << endl;
        if(length >= 1) {
            cout << "2. copyCar      复制已有小车" << endl;
        }
        cin >> cmd;
        if(cmd == "1" || cmd == DIRECT_CREATE) {
            SmartCar* newCar = carController->createCar();
            break;
        }
        else if(cmd == "2" || cmd == COPY_CAR) {
            SmartCar* car = getCarByNum();
            SmartCar* newCar = carController->createCar(car);
            break;
        }
        cout << "输入错误, 请输入指令或指令序号!" << endl;
    }
    cout << "创建成功！" << endl;
}
void listCar() {
    int length = carController->getCarCount();
    SmartCar* carList[length];
    carController->listAllCar(carList);
    cout << "共有" << length << "辆小车:" << endl;
    for(int i=0;i<length;i++) {
        cout <<  i+1 << " : " << carList[i]->getId() << endl;
    }
    cout << endl;
}
void editCar() {
    SmartCar* car = getCarByNum();
    if(car == nullptr) return;
    string cmd;
    while(cmd != E_X_I_T) {
        cmd = "";
        cout << "======"<< car->getId() << "小车编辑界面=========" << endl;
        cout << "指令说明:" << endl;
        cout << "1. createComponent 创建组件" << endl;
        cout << "2. listComponent   列出所有组件名称" << endl;
        cout << "3. editComponent   编辑组件" << endl;
        cout << "4. deleteComponent 删除组件" << endl;
        cin >> cmd;
        if(cmd == "1" || cmd == CREATE_COMPONENT) {
            createComponent(car);
        }
        else if(cmd == "2" || cmd == LIST_COMPONENT) {
            listComponent(car);
        }
        else if(cmd == "3" || cmd == EDIT_COMPONENT) {
            editComponent(car);
        }
        else if(cmd == "4" || cmd == DELETE_COMPONENT) {
            deleteComponent(car);
        }
    }
    cout << car->getId() << "小车编辑完成。" << endl;
}
void deleteCar() {
    SmartCar* car = getCarByNum();
    string carId = car->getId();
    if(car == nullptr) return;
    carController->deleteCar(car);
    cout << carId << "小车已删除。" << endl;
}
void storeCarData() {
    int length = carController->getCarCount();
    SmartCar* carList[length];
    carController->listAllCar(carList);
    ofstream fout(CarDataPath);
    for(int i=0;i<length;i++) {
        fout << carList[i]->toString() << endl;
        cout << carList[i]->toString() << endl;
    }
    fout.close();
    cout << "小车数据保存完成" << endl;
    return;
}
void loadCarData() {
    ifstream fin(CarDataPath);
    string line;
    while(getline(fin,line)) {
        istringstream iss(line);
        SmartCar* car = carController->strToSmartCar(iss);
    }
    fin.close();
    cout << "小车数据加载完成" << endl;
    return;
}


Component* getComponentByNum(SmartCar* car) {
    int length = car->getComponentCount();
    Component* componentList[length];
    car->listAllComponent(componentList);
    cout << "共有" << length << "个组件:" << endl;
    for(int i=0;i<length;i++) {
        cout << i+1 << " : " << componentList[i]->getName() << endl;
    }

    int componentNum = -1;
    while(true) {
        cout << "请输入组件序号:";
        cin >> componentNum;
        cout << endl;
        componentNum -= 1;
        if(componentNum >= 0 && componentNum <length) {
            break;
        }
        cout << "序号有误" << endl;
    }
    return componentList[componentNum];
}
void createComponent(SmartCar* car) {
    cout << "请输入组件名称:";
    string componentName;
    cin >> componentName;
    car->addComponent(componentName);
    cout << "组件创建成功!" << endl;
}
void listComponent(SmartCar* car) {
    int length = car->getComponentCount();
    Component* componentList[length];
    car->listAllComponent(componentList);
    cout << car->getId() << "小车共有" << length << "个组件:" << endl;
    for(int i=0;i<length;i++) {
        cout << i+1 << " : " << componentList[i]->getName() << endl;
    }
}
void editComponent(SmartCar* car) {
    Component* component = getComponentByNum(car);
    if(component == nullptr) return;
    string cmd;
    while(cmd != E_X_I_T) {
        cmd = "";
        cout << "======"<< component->getName() << "组件编辑界面=========" << endl;
        cout << "指令说明:" << endl;
        cout << "1. createParam     创建参数          | 6. createComponent 创建组件" << endl;
        cout << "2. createIdParam   创建ID            | 7. listComponent   列出所有组件名称" << endl;
        cout << "3. listParam       列出所有参数名称   | 8. editComponent   编辑组件" << endl;
        cout << "4. editParam       编辑参数          | 9. deleteComponent 删除组件" << endl;
        cout << "5. deleteParam     删除参数" << endl;
        cin >> cmd;
        if(cmd == "1" || cmd == CREATE_PARAM) {
            createParam(component);
        }
        else if(cmd == "2" || cmd == CREATE_ID_PARAM) {
            createIdParam(component);
        }
        else if(cmd == "3" || cmd == LIST_PARAM) {
            listParam(component);
        }
        else if(cmd == "4" || cmd == EDIT_PARAM) {
            editParam(component);
        }
        else if(cmd == "5" || cmd == DELETE_PARAM) {
            deleteParam(component);
        }
        else if(cmd == "6" || cmd == CREATE_COMPONENT) {
            createComponent(component);
        }
        else if(cmd == "7" || cmd == LIST_COMPONENT) {
            listComponent(component);
        }
        else if(cmd == "8" || cmd == EDIT_COMPONENT) {
            editComponent(component);
        }
        else if(cmd == "9" || cmd == DELETE_COMPONENT) {
            deleteComponent(component);
        }
        cout << "=======================" << endl;
    }
    cout << car->getId() << "组件编辑完成。" << endl;
}
void deleteComponent(SmartCar* car) {
    Component* component = getComponentByNum(car);
    string componentName = component->getName();
    if(component == nullptr) return;
    car->deleteComponent(componentName);
    cout << car->getId() << "小车的 " << componentName << " 组件已删除。" << endl;
}

Param* getParamByNum(Component* component) {
    int length = component->getParamCount();
    Param* paramList[length];
    component->listAllParam(paramList);
    cout << "共有" << length << "个参数:" << endl;
    for(int i=0;i<length;i++) {
        cout << i+1 << " : " << paramList[i]->getName() << endl;
    }

    int paramNum = -1;
    while(true) {
        cout << "请输入参数序号:";
        cin >> paramNum;
        cout << endl;
        paramNum -= 1;
        if(paramNum >= 0 && paramNum <length) {
            break;
        }
        cout << "序号有误" << endl;
    }
    return paramList[paramNum];
}
void createParam(Component* component) {
    cout << "请输入参数名称:";
    string paramName;
    cin >> paramName;
    component->addParam(paramName);
    cout << "参数创建成功!" << endl;
}
void createIdParam(Component* component) {
    cout << "请输入参数名称:";
    string paramName;
    cin >> paramName;
    
    Param* param = component->addParam(paramName);

    cout << "请输入ID前缀" << endl;
    string front;
    cin >> front;
    cout << "请输入ID总长度" << endl;
    int length;
    cin >> length;
    // TODO 输入错误处理

    IdManager* idManager = IdManager::getIdManager();
    idManager->registered(component->getName() + param->getName(),front,length);
    param->thisIsId();

    string id = idManager->createId(component->getName() + param->getName());
    param->setValue(id);

    cout << "参数创建成功!" << endl;
}
void listParam(Component* component) {
    int length = component->getParamCount();
    Param* paramList[length];
    component->listAllParam(paramList);
    cout << component->getName() << "组件共有" << length << "个子参数:" << endl;
    cout << "序号 " << " 参数名 " << " 参数单位 " << " 参数值 " << " 参数备注 " << endl; 
    for(int i=0;i<length;i++) {
        cout << i+1 << " : " << paramList[i]->getName();
        cout << " " << paramList[i]->getUnit();
        cout << " " << paramList[i]->getValue();
        cout << " " << paramList[i]->getRemark();
        cout << endl;
    }
}
void editParam(Component* component) {
    Param* param = getParamByNum(component);
    if(param == nullptr) return;
    string cmd;
    while(cmd != E_X_I_T) {
        cmd = "";
        cout << "======"<< param->getName() << "参数编辑界面=========" << endl;
        cout << "指令说明:" << endl;
        cout << "1. editValue       编辑参数值" << endl;
        cout << "2. editUnit        编辑参数单位" << endl;
        cout << "3. editRemark      编辑备注" << endl;
        cin >> cmd;
        if(cmd == "1" || cmd == EDIT_VALUE) {
            editValue(param);
        }
        else if(cmd == "2" || cmd == EDIT_UNIT) {
            editUnit(param);
        }
        else if(cmd == "3" || cmd == EDIT_REMARK) {
            editRemark(param);
        }
        cout << "=======================" << endl;
    }
}
void deleteParam(Component* component) {
    Param* param = getParamByNum(component);
    string paramName = param->getName();
    if(param == nullptr) return;
    component->deleteParam(paramName);
    cout << component->getName() << "组件的 " << paramName << " 参数已删除。" << endl;
}

Component* getComponentByNum(Component* component) {
    int length = component->getSubComponentCount();
    Component* subComponentList[length];
    component->listAllSubComponent(subComponentList);
    cout << "共有" << length << "个子组件:" << endl;
    for(int i=0;i<length;i++) {
        cout << i+1 << " : " << subComponentList[i]->getName() << endl;
    }

    int subComponentNum = -1;
    while(true) {
        cout << "请输入子组件序号:";
        cin >> subComponentNum;
        cout << endl;
        subComponentNum -= 1;
        if(subComponentNum >= 0 && subComponentNum <length) {
            break;
        }
        cout << "序号有误" << endl;
    }
    return subComponentList[subComponentNum];
}
void createComponent(Component* component) {
    cout << "请输入子组件名称:";
    string subComponentName;
    cin >> subComponentName;
    component->addSubComponent(subComponentName);
    cout << "子组件创建成功!" << endl;
}
void listComponent(Component* component) {
    int length = component->getSubComponentCount();
    Component* subComponentList[length];
    component->listAllSubComponent(subComponentList);
    cout << component->getName() << "组件共有" << length << "个子组件:" << endl;
    for(int i=0;i<length;i++) {
        cout << i+1 << " : " << subComponentList[i]->getName() << endl;
    }
}
void editComponent(Component* component) {
    Component* subComponent = getComponentByNum(component);
    if(subComponent == nullptr) return;
    string cmd;
    while(cmd != E_X_I_T) {
        cmd = "";
        cout << "======"<< subComponent->getName() << "组件编辑界面=========" << endl;
        cout << "指令说明:" << endl;
        cout << "1. createParam 创建参数            | 5. createComponent 创建组件" << endl;
        cout << "2. listParam   列出所有参数名称     | 6. listComponent   列出所有组件名称" << endl;
        cout << "3. editParam   编辑参数            | 7. editComponent   编辑组件" << endl;
        cout << "4. deleteParam 删除参数            | 8. deleteComponent 删除组件" << endl;
        cin >> cmd;
        if(cmd == "1" || cmd == CREATE_PARAM) {
            createParam(subComponent);
        }
        else if(cmd == "2" || cmd == LIST_PARAM) {
            listParam(subComponent);
        }
        else if(cmd == "3" || cmd == EDIT_PARAM) {
            editParam(subComponent);
        }
        else if(cmd == "4" || cmd == DELETE_PARAM) {
            deleteParam(subComponent);
        }
        else if(cmd == "5" || cmd == CREATE_COMPONENT) {
            createComponent(subComponent);
        }
        else if(cmd == "6" || cmd == LIST_COMPONENT) {
            listComponent(subComponent);
        }
        else if(cmd == "7" || cmd == EDIT_COMPONENT) {
            editComponent(subComponent);
        }
        else if(cmd == "8" || cmd == DELETE_COMPONENT) {
            deleteComponent(subComponent);
        }
        cout << "=======================" << endl;
    }
    cout << subComponent->getName() << "组件编辑完成。" << endl;
}
void deleteComponent(Component* component) {
    Component* subComponent = getComponentByNum(component);
    string subComponentName = subComponent->getName();
    if(subComponent == nullptr) return;
    component->deleteSubComponent(subComponentName);
    cout << component->getName() << "组件的 " << subComponentName << " 组件已删除。" << endl;
}

void editValue(Param* param) {
    cout << "======"<< param->getName() << "参数值编辑界面=========" << endl;
    string valueBefore = param->getValue();
    cout << "请输入参数值:" << endl;
    string value;
    cin >> value;
    param->setValue(value);
    cout << param->getName() << "参数的值已由 " << valueBefore << " 修改为 "<< value << endl;
    cout << "=======================" << endl;
}
void editUnit(Param* param) {
    cout << "======"<< param->getName() << "参数单位编辑界面=========" << endl;
    string unitBefore = param->getUnit();
    cout << "请输入参数单位:" << endl;
    string unit;
    cin >> unit;
    param->setUnit(unit);
    cout << param->getName() << "参数的单位已由 " << unitBefore << " 修改为 "<< unit << endl;
    cout << "=======================" << endl;
}
void editRemark(Param* param) {
    cout << "======"<< param->getName() << "参数备注编辑界面=========" << endl;
    string remarkBefore = param->getRemark();
    cout << "请输入参数备注:" << endl;
    string remark;
    getline(cin,remark);
    param->setRemark(remark);
    cout << param->getName() << "参数的备注已由 " << remarkBefore << " 修改为 "<< remark << endl;
    cout << "=======================" << endl;
}